package CaesarCipher;

import javafx.fxml.FXML;
import javafx.scene.control.*;
        import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Map;

public class UIController {

    @FXML
    private TextField inputFilePath;
    @FXML
    private TextField outputFilePath;
    @FXML
    private TextField shiftKey;
    @FXML
    private TextArea plaintextArea;
    @FXML
    private TextArea ciphertextArea;
    @FXML
    private TextArea decryptedTextArea;
    @FXML
    private Label statusLabel;

    private CaesarCipher cipher;
    private FrequencyCounter frequencyCounter;
    private BruteForceAttack bruteForceAttack;

    public UIController() {
        cipher = new CaesarCipher();
        frequencyCounter = new FrequencyCounter();
        bruteForceAttack = new BruteForceAttack();
    }

    @FXML
    private void handleEncrypt() {
        try {
            String plaintext = cipher.readFile(inputFilePath.getText());
            plaintextArea.setText(plaintext);
            int shift = Integer.parseInt(shiftKey.getText());
            String ciphertext = cipher.encrypt(plaintext, shift);
            ciphertextArea.setText(ciphertext);

            // 显示字符频率
            Map<Character, Integer> plaintextFreq = cipher.countFrequency(plaintext);
            Map<Character, Integer> ciphertextFreq = cipher.countFrequency(ciphertext);
            displayFrequency("加密前明文频率:", plaintextFreq, plaintextArea);
            displayFrequency("加密后密文频率:", ciphertextFreq, ciphertextArea);

        } catch (Exception e) {
            statusLabel.setText("加密失败: " + e.getMessage());
        }
    }

    @FXML
    private void handleDecrypt() {
        try {
            String ciphertext = ciphertextArea.getText();
            int shift = Integer.parseInt(shiftKey.getText());
            String decryptedText = cipher.decrypt(ciphertext, shift);
            decryptedTextArea.setText(decryptedText);

            // 显示字符频率
            Map<Character, Integer> decryptedFreq = cipher.countFrequency(decryptedText);
            displayFrequency("解密后明文频率:", decryptedFreq, decryptedTextArea);

        } catch (Exception e) {
            statusLabel.setText("解密失败: " + e.getMessage());
        }
    }

    @FXML
    private void handleBruteForce() {
        try {
            String ciphertext = ciphertextArea.getText();
            String originalTextSample = plaintextArea.getText(); // 假设明文样本为已输入的明文
            String decryptedText = bruteForceAttack.bruteForce(ciphertext, originalTextSample);
            decryptedTextArea.setText(decryptedText);

            // 显示字符频率
            Map<Character, Integer> decryptedFreq = cipher.countFrequency(decryptedText);
            displayFrequency("穷举解密后明文频率:", decryptedFreq, decryptedTextArea);

        } catch (Exception e) {
            statusLabel.setText("穷举攻击失败: " + e.getMessage());
        }
    }

    // 显示字符频率
    private void displayFrequency(String title, Map<Character, Integer> frequencyMap, TextArea textArea) {
        TextArea frequencyArea = new TextArea();
        frequencyArea.setEditable(false);
        frequencyArea.setText(title + "\n");
        for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
            frequencyArea.appendText(entry.getKey() + ": " + entry.getValue() + "\n");
        }

        // 创建一个新的Stage来显示频率
        Stage stage = new Stage();
        stage.setTitle(title);
        stage.setScene(new javafx.scene.Scene(new javafx.scene.layout.VBox(frequencyArea), 400, 300));
        stage.show();
    }
}